package com.nextgen.services;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.nextgen.bean.Asin;
import com.nextgen.mongo.core.MongoInstance;
  
/**  
 * The Class AsinService.  
 */  
@Service  
public class AsinService {  
  
     private static final Logger logger_c = Logger.getLogger(AsinService.class);  
  
     /**  
      * Get the asin by id.  
      *  
      * @param asinId_p  
      *      the asin id_p  
      * @return the asin by id  
      */  
	public Asin getAsinById(String asinId_p) {
		MongoOperations mongoOperation = MongoInstance.getInstance();
		Query searchUserQuery = new Query(Criteria.where("asinId").is(asinId_p));
		
		// find the saved user again.
		Asin asin = mongoOperation.findOne(searchUserQuery, Asin.class);
		System.out.println("Asin details with id : " + asinId_p + " is : "  + asin);
		return asin;
	}
  
     /**  
      * Gets all asins.  
      *  
      * @return the all asins  
      */  
     public List<Asin> getAllAsins() {
    	 MongoOperations mongoOperation = MongoInstance.getInstance();
    	 List<Asin> asins = mongoOperation.findAll(Asin.class);
    	 
    	 return asins;  
     }
     
     /**  
      * Gets all asins.  
      *  
      * @return the all asins  
      */  
     public List<Asin> getAllAsinsForTheSellerBasedOnCategory(String sellerId_p, List<String> categories) {
    	 MongoOperations mongoOperation = MongoInstance.getInstance();
		Query searchUserQuery = new Query(
				Criteria.where("asinSellerId").is(sellerId_p)
				.andOperator(
						Criteria.where("asinCategory").in(categories)));
    	 List<Asin> asins = mongoOperation.find(searchUserQuery, Asin.class);
    	 
    	 return asins;  
     }
     /**  
      * Creates the asin.  
      *  
      * @param asin_p  
      *      the asin_p  
      * @return the asin  
      */  
     public Asin createAsin(Asin asin) {  
  
          logger_c.debug("Persisting asin in database: " + asin.toString());  
          MongoOperations mongoOperation = MongoInstance.getInstance();
          mongoOperation.save(asin);  
  
          return asin;  
     }  
  
     /**  
      * Update asin.  
      *  
      * @param asin_p  
      *      the asin_p  
      * @return the asin  
      */  
     public Asin updateAsin(Asin asin_p) {  
  
          logger_c.debug("Updating asin in database: " + asin_p.toString());  
          asin_p.setLastUpdated(new Date());
          MongoOperations mongoOperation = MongoInstance.getInstance();
          Query searchUserQuery = new Query(Criteria.where("asinId").is(asin_p.getAsinId()));
          mongoOperation.updateFirst(searchUserQuery, 
                  Update.update("password", "new password"),Asin.class);
  
          return asin_p;  
     }  
  
     /**  
      * Delete asin.  
      *  
      * @param asinId_p  
      *      the asin id_p  
      */  
     public void deleteAsin(String asinId_p) {  
          logger_c.debug("Deleting asin from database: " + asinId_p.toString());
          MongoOperations mongoOperation = MongoInstance.getInstance();
          Query searchUserQuery = new Query(Criteria.where("asinId").is(asinId_p));
          mongoOperation.remove(searchUserQuery, Asin.class);
     }  
}